<?php
// +----------------------------------------------------------------------
// | 老汉的项目
// +----------------------------------------------------------------------

namespace app\admin\model;
use think\Model;

class Flow extends Model
{
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    //插入数据
    public function saveflow() {
        $self = new self;
        $self->status=isMobile()?1:0;
        $self->ip=get_client_ip();
        $self->save();
    }

    //获取时间段用的
    public function getDaysList($starttime,$endtime,$type=1)
    {
        $starttime = $starttime?$starttime:date('Y-m-d',time()-(24*3600*6));
        $endtime = $endtime?$endtime:date('Y-m-d');
        $arr=periodDate($starttime,$endtime,$type);
        return $arr;
    }

    //获取时间段小时
    public function getHoursList($starttime,$endtime)
    {
        $starttime = $starttime?$starttime:date('Y-m-d',time()).' 06:00:00';
        $endtime = $endtime?$endtime:date('Y-m-d').' 23:59:59';
        $arr=periHourDate($starttime,$endtime);
        return $arr;
    }

    //获取表格数据用的
    public function getChartList($days)
    {
        if(count($days)==0){
            return [];
        }
        $arr = [];
        foreach($days as $k => $v){
            // 时间区间查询
            $where = [];
            $where['createtime'] = ['between time',[$v['use'],$v['use'].' 23:23:59']];

            $arr['computer'][]=self::where($where)->where(['status'=>0])->count();

            $arr['phone'][]=self::where($where)->where(['status'=>1])->count();
        }
        return $arr;
    }

    //获取表格数据用的小时
    public function getHourChartList($days)
    {
        if(count($days)==0){
            return [];
        }
        $arr = [];
        foreach($days as $k => $v){
            // 时间区间查询
            $where = [];
            $end = date('Y-m-d H:i:s',strtotime($v['use'])+60*60-1);
            $where['createtime'] = ['between time',[$v['use'],$end]];

            $arr['computer'][]=self::where($where)->where(['status'=>0])->count();

            $arr['phone'][]=self::where($where)->where(['status'=>1])->count();
        }
        return $arr;
    }

}
